/**
 @file ctc_asw_common.h

 @author  Copyright (C) 2020 Centec Networks Inc.  All rights reserved.

 @date 2020-06-15

 @version v1.0

 The file define  CENTEC SDK  common
*/
#ifndef _CTC_ASW_COMMON_H
#define _CTC_ASW_COMMON_H
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************
*
* Header Files
*
****************************************************************************/
#include "sal.h"
#include "ctc_common.h"
#include "ctc_mix.h"
#include "ctc_error.h"
/***************************************************************************
*
* Defines and Macros
*
****************************************************************************/
#define CTC_ASW_MAX_PORT_NUM_PER_CHIP        29
#define CTC_ASW_MAX_PHY_PORT        28
#define CTC_ASW_INTERNAL_PHY_PORT_NUM 8
#define CTC_ASW_LAG_BASE            56
#define CTC_ASW_MAX_VLAN          4095
#define CTC_ASW_MAX_STATS_ID      191  /*255-64(reserver for policer)*/
#define CTC_ASW_QOS_PORT_CELL_MIN_PROFILE_MIRROR    7
#define CTC_ASW_QOS_PORT_CELL_MIN_PROFILE    6
#define CTC_ASW_SPMAC_KEY_NUM 38
#define CTC_ASW_PMAC_KEY_NUM 16
#define CTC_ASW_MAX_SPMAC_GROUP 16
#define CTC_ASW_L2_ACL_ENTRY_MAX  127

#define CTC_L2_IP_IS_MCAST(val) \
{\
    if ((val >> 28 & 0xE)!=0xE){ \
       return CTC_E_INVALID_PARAM; } \
}

#define CTC_L2_MAC_IS_MCAST(val) \
    {\
        if ((val & 0x1)!=0x01){ \
           return CTC_E_INVALID_PARAM; } \
    }


#define CTC_L2_MAC_IS_UCAST(val) \
        {\
            if ((val & 0x1)==0x01){ \
               return CTC_E_INVALID_PARAM; } \
        }

#define IPMC_MASK_IPV4_ADDR(ip, len)  \
    do                          \
    {                           \
        uint32 mask = (len) ? ~((1 << (CTC_IPV4_ADDR_LEN_IN_BIT - (len))) - 1) : 0; \
        (ip) &= mask;             \
    } while (0)

/* define lock operations  */
extern sal_mutex_t* api_mutex[CTC_MAX_LOCAL_CHIP_NUM];
extern sal_mutex_t* mdio_mutex[CTC_MAX_LOCAL_CHIP_NUM];
#define CTC_API_LOCK(lchip)  \
{ \
    if(lchip>=CTC_MAX_LOCAL_CHIP_NUM) \
        return CTC_E_INVALID_CHIP_ID; \
    if(NULL == api_mutex[lchip]) \
        return CTC_E_NOT_INIT; \
    sal_mutex_lock(api_mutex[lchip]); \
}

#define CTC_API_LOCK_THREAD(lchip)  \
{ \
    if(lchip>=CTC_MAX_LOCAL_CHIP_NUM) \
        return ; \
    if(NULL == api_mutex[lchip]) \
        return ; \
    sal_mutex_lock(api_mutex[lchip]); \
}

#define CTC_API_UNLOCK(lchip) sal_mutex_unlock(api_mutex[lchip])

#define CTC_API_UNLOCK_THREAD(lchip) sal_mutex_unlock(api_mutex[lchip])

#define CTC_MDIO_LOCK(lchip)  \
{ \
    if(lchip>=CTC_MAX_LOCAL_CHIP_NUM) \
        return CTC_E_INVALID_CHIP_ID; \
    if(NULL == mdio_mutex[lchip]) \
        return CTC_E_NOT_INIT; \
    sal_mutex_lock(mdio_mutex[lchip]); \
}

#define CTC_MDIO_UNLOCK(lchip) sal_mutex_unlock(mdio_mutex[lchip])

#undef CTC_ERROR_RETURN
#undef CTC_ERROR_GOTO

#ifdef SDK_IN_DEBUG_VER
#define CTC_ERROR_RETURN(op) \
			{ \
				int32 rv = (op); \
				if (rv < 0) \
				{ \
					if (g_error_on) \
					sal_printf("Error Happened!! Fun:%s()  Line:%d ret:%d\n",__FUNCTION__,__LINE__, rv); \
					return (rv); \
				} \
			}

#define CTC_ERROR_GOTO(op, ret, label) \
						{ \
							ret = (op); \
							if (ret < 0) \
							{ \
								if (g_error_on) \
								sal_printf("Error Happened!! Fun:%s()  Line:%d\n",__FUNCTION__,__LINE__); \
								goto label; \
							} \
						}

#define CTC_ERROR_RETURN_UNLOCK(op) \
		{ \
			int32 rv; \
			if ((rv = (op)) < 0) \
			{ \
			    sal_printf("Error Happened!! Fun:%s()  Line:%d\n",__FUNCTION__,__LINE__); \
				CTC_API_UNLOCK(lchip); \
				return (rv); \
			}; \
		}
#else
#define CTC_ERROR_RETURN(op) \
			{ \
				int32 rv = (op); \
				if (rv < 0) \
				{ \
					return (rv); \
				} \
			}

#define CTC_ERROR_GOTO(op, ret, label) \
						{ \
							ret = (op); \
							if (ret < 0) \
							{ \
								goto label; \
							} \
						}

#define CTC_ERROR_RETURN_UNLOCK(op) \
		{ \
			int32 rv; \
			if ((rv = (op)) < 0) \
			{ \
				CTC_API_UNLOCK(lchip); \
				return (rv); \
			}; \
		}
#endif

#define CTC_MAX_VALUE_CHECK_UNLOCK(var, max_value) \
    { \
        if ((var) > (max_value)) \
        { \
            CTC_API_UNLOCK(lchip); \
            return CTC_E_INVALID_PARAM; \
        } \
    }

#define CTC_ASW_FID_RANGE_CHECK(val) \
    { \
        if ( (val) > 4095){ \
            return CTC_E_INVALID_PARAM; } \
    }

#define LCHIP_CHECK(lchip)                                  \
    do {                                                    \
        if ((lchip) >= g_lchip_num){                        \
            return CTC_E_INVALID_CHIP_ID; }            \
    } while (0)

#define CTC_MAX_GPORT_CHECK(var) \
    { \
        if (!CTC_IS_LINKAGG_PORT(var)) \
        { \
            var = CTC_MAP_GPORT_TO_LPORT(var); \
            CTC_MAX_VALUE_CHECK(var, CTC_ASW_MAX_PHY_PORT); \
        } \
        else\
        { \
            CTC_MAX_VALUE_CHECK(var, 0x1F04);\
        } \
    }

#define CTC_MAX_QOS_DSCP_VALUE  63   /**< Max qos dscp value */

#define INTERNAL

#define CTC_ASW_SET_USER_MAC(dest, src)     \
    {   \
        (dest)[0] = (src)[1] >> 8;          \
        (dest)[1] = (src)[1] & 0xFF;        \
        (dest)[2] = (src)[0] >> 24;         \
        (dest)[3] = ((src)[0] >> 16) & 0xFF;\
        (dest)[4] = ((src)[0] >> 8) & 0xFF; \
        (dest)[5] = (src)[0] & 0xFF;        \
    }
#define CTC_ASW_SET_HW_MAC(dest, src)     \
    {   \
        (dest)[1]= (((src)[0] << 8)  | ((src)[1]));        \
        (dest)[0]= (((src)[2] << 24) | ((src)[3] << 16) | ((src)[4] << 8) | ((src)[5])); \
    }
#define CTC_ASW_REVERT_IP6(dest, src)     \
    {                                            \
        (dest)[3] = (src)[0];                        \
        (dest)[2] = (src)[1];                        \
        (dest)[1] = (src)[2];                        \
        (dest)[0] = (src)[3];                        \
    }
#define FDB_SET_HW_MAC(d, s)    CTC_ASW_SET_HW_MAC(d, s)
/***************************************************************************
*
*  Functions
*
****************************************************************************/


#ifdef __cplusplus
}
#endif

#endif

